/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 *  Copyright 2008 Global Sign In
 * 
 *  This code is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _MESSAGEDETAILS_H_
#define _MESSAGEDETAILS_H_

#include <set>
#include <vector>
#include <string>

#include "Substitute.h"

/// An attachment.
class Attachment
{
	public:
		Attachment(const std::string &fileName,
			const char *pContent, unsigned long contentLen);
		virtual ~Attachment();

		std::string m_fileName;
		std::string m_headers;
		char *m_pEncodedContent;
		unsigned long m_encodedLength;

	private:
		Attachment(const Attachment &other);
		Attachment &operator=(const Attachment &other);

};

/// A message's details.
class MessageDetails
{
	public:
		MessageDetails();
		virtual ~MessageDetails();

		/**
		  * Loads the contents of the file raw.
		  * Caller frees with delete[].
		  */
		static char *loadRaw(const std::string &filePath, int &fileSize);

		/// Generates the string to substitute {Recipient ID} with.
		static std::string encodeRecipientId(const std::string &recipientId);

		/// Generates a message Id.
		std::string createMessageId(const std::string &suffix);

		/// Gets a Substitute object for the subject.
		Substitute *getSubjectSubstituteObj(bool hasFields = true);

		/// Gets a Substitute object for plain text content.
		Substitute *getPlainSubstituteObj(bool hasFields = true);

		/// Gets a Substitute object for HTML content.
		Substitute *getHtmlSubstituteObj(bool hasFields = true);

		/// Returns whether the message has to be personalized for each recipient.
		bool isRecipientPersonalized(void) const;
 
		/// Sets the list of files to attach to the message.
		void setAttachments(const std::vector<std::string> &filePaths);

		/// Sets the attachment array.
		void setAttachmentArray(const std::string &serializedArray);

		/// Gets the attachment array.
		std::string getAttachmentArray(void) const;

		/// Returns the number of attachments.
		unsigned int getAttachmentCount(void) const;

		/// Gets headers for an attachment.
		std::string getAttachmentHeaders(unsigned int attachmentNum);

		/// Gets an attachment in encoded form.
		const char *getEncodedAttachment(unsigned int attachmentNum, unsigned long &dataLen) const;

		std::set<std::string> m_fieldNames;
		std::string m_subject;
		std::string m_to;
		std::string m_cc;
		std::string m_fromName;
		std::string m_fromEmailAddress;
		std::string m_replyToName;
		std::string m_replyToEmailAddress;
		std::string m_senderEmailAddress;
		std::string m_plainContent;
		std::string m_htmlContent;
		bool m_personalizeSubject;
		bool m_personalizePlain;
		bool m_personalizeHtml;
		std::string m_unsubscribeLink;

	protected:
		Substitute *m_pSubjectSub;
		Substitute *m_pPlainSub;
		Substitute *m_pHtmlSub;
		bool m_isRecipientPersonalized;
		std::string m_attachmentArray;
		std::vector<Attachment *> m_attachments;

		/// Creates a new Substitute object.
		Substitute *newSubstitute(const std::string &contentTemplate, bool hasFields,
			bool escapeEntities, bool encodeQP);

	private:
		MessageDetails(const MessageDetails &other);
		MessageDetails &operator=(const MessageDetails &other);

};

#endif // _MESSAGEDETAILS_H_
